
var app = getApp()
var util = require('.../../../../../utils/util')

Page({
  // tab切换
  tabClick(e){
    this.setData({
      product_name:'',
      page:1,
      isSelect:false,
      currentTab: e.currentTarget.dataset.type,
      goodsLists:[]
    })
    this.getGoodsLists();
  },
  // 是否全选
  isSelectTap(){
    // 全选
    var goodsLists = this.data.goodsLists.map(val=>{
      val.selected = !this.data.isSelect
      return val
    })
    this.setData({
      goodsLists:goodsLists,
      isSelect: !this.data.isSelect
    })
  },
  // 批量下架
  allDown(){
    this.setData({
      isSelect:false
    })
    var ids = this.data.goodsLists.filter(val=>val.selected).map(val=>val.id)
    this.downUpAjax(2,ids.join(','))
  },
  // 批量上架
  allUp(){
    var ids = this.data.goodsLists.filter(val=>val.selected).map(val=>val.id)
    this.setData({
      isSelect:false
    })
    this.downUpAjax(1,ids.join(','))
  },
  // 批量删除
  allDelete(){
    this.setData({
      isSelect:false
    })
    var ids = this.data.goodsLists.filter(val=>val.selected).map(val=>val.id)
    this.deleteGoodsAjax(ids.join(','));
  },
  // 改价模板
  changePriceTemplate(){
    var that = this;
    wx.request({
      url: app.globalData.urls + '/product/price-template',
      method: 'GET',
      data: {
        bdmin_user_id:util.getBdmin_user_id()
      },
      header: app.getPostRequestHeader(),
      success: function (res) {
        if(res.data.code==200){
          let data = res.data.data;
          that.setData({
            template:data,
            price:data[0]
          })
        }
      }
    })
  },
  // 价格
  changePriceItem(e){
    if(e.type=="change"){
      this.setData({
        price:this.data.template[e.detail.value[0]]
      })
    }else{
      this.setData({
        price:e.target.dataset.item
      })
    }
  },
  // 批量改价
  changePrice(){
    this.setData({
      hideLogPopup:false,
      popus:3
    })
  },
  // 搜索
  self(){
    this.setData({
      dropdownLists:this.data.selfDropdown,
      hideLogPopup:false,
      popus:1
    })
  },
  goodsType(){
    this.setData({
      hideLogPopup:false,
      popus:2
    })
  },
  /**
   * 页面的初始数据
   */
  data: {
    price:{},
    template:[],
    currentTab:0,
    isSelect:false,
    page:1,
    product_name:"",
    sort:"created_at",//时间created_at 销量sale_num
    isnew:'',
    is_hot:'',
    hideLogPopup:true,
    dropdownLists:[],
    selfType:3,
    selfname:"商品库",
    selfDropdown:[{label:1,name:"电商"},{label:2,name:"团购"} ,{label:3,name:"商品库"}],
    category:[[],[],[]],
    categoryId:[[],[],[]],
    selectCate:["","","未选择"],
    popus:1, // 1 -- 商品库
    goodsLists:[]
  },
  // 删除item
  deleteItem(e){
    let item= e.currentTarget.dataset.item;
    this.deleteGoodsAjax(item.id);
  },
  deleteGoodsAjax(ids){
    var that=this;
    this.setData({
      page:1
    })
    wx.request({
      url: app.globalData.urls + '/product/product-del',
      method: 'POST',
      data: {
        bdmin_user_id:util.getBdmin_user_id(),
        product_ids:ids
      },
      header: app.getPostRequestHeader(),
      success: function (res) {
        if(res.data.code==200){
          that.setData({
            page:1,
            goodsLists:[],
            isSelect:false,
          })
          that.getGoodsLists();
        }
      }
    })
  },
  downUpAjax(type,ids){
    var that =this;
    wx.request({
      url: app.globalData.urls + '/product/product-shelf',
      method: 'POST',
      data: {
        bdmin_user_id:util.getBdmin_user_id(),
        product_ids:ids,
        status: type //上下架状态 1 上架 2下架
      },
      header: app.getPostRequestHeader(),
      success: function (res) {
        if(res.data.code==200){
          that.setData({
            page:1,
            goodsLists:[],
            isSelect:false,
          })
          that.getGoodsLists();
        }else{
          wx.showToast({
            title: res.data.message,
            icon:'none'
          })
        }
      }
    })
  },
  // 下架
  down(e){
    let item= e.currentTarget.dataset.item;
    this.downUpAjax(2,item.id)
  },
  // 上架
  up(e){
    let item= e.currentTarget.dataset.item;
    this.downUpAjax(1,item.id)
  },
  // 三级联动
  selectCity(e){
    if(e.type=="change"){
      let v=e.detail.value
      let c=this.data.category;

      this.getCategory(1,c[0][v[0]].id)
      setTimeout(()=>{
        if(c[1][v[1]]) this.getCategory(2,c[1][v[1]].id)
      },100)
      
      setTimeout(()=>{
        this.setData({
          getLastIndex:v,
          selectCate:[c[0][v[0]].name,c[1][v[1]] && c[1][v[1]].name || "",c[2][v[2]] &&c[2][v[2]].name||'']
        })
      },200)
    }else{
      let dataset = e.target.dataset;
      if(dataset.type!=3){
        this.getCategory(dataset.type,dataset.item.id)
      }
      this.setData({
        ["selectCate["+(dataset.type-1)+"]"]:dataset.item.name
      })
    }
  },
  // 弹窗确定按钮
  confrim(){
    if(this.data.popus==2){
      if(this.data.selectCate[2]=='未选择'){
        this.setData({
          ["selectCate[2]"]:this.data.category[2][0].name
        })
      }else{
        if(this.data.getLastIndex){
          this.setData({
            ["selectCate[2]"]:this.data.category[2][this.data.getLastIndex[2]].name
          })
        }
      }
    }
    
    if(this.data.popus==3){
      var that = this;
      var ids = this.data.goodsLists.filter(val=>val.selected).map(val=>val.id)
      wx.request({
        url: app.globalData.urls + '/product/price-modify',
        method: 'POST',
        data: {
          bdmin_user_id:util.getBdmin_user_id(),
          product_ids:ids.join(','),
          price_id:this.data.price.id
        },
        header: app.getPostRequestHeader(),
        success: function (res) {
          if(res.data.code==200){
            that.setData({
              page:1,
              isSelect:false,
              goodsLists:[]
            })
            that.getGoodsLists()
          }
        }
      })
    }else{
      console.log(1111)
      this.setData({
        page:1,
        isSelect:false,
        goodsLists:[],
        isnew:'',
        is_hot:''
      })
      this.getGoodsLists()
    }
    
    this.closeLogPopupTap();
  },
  // 弹窗切换
  changeTap(e){
    let item = e.currentTarget.dataset.item;
    this.setData({
      selfType:item.label,
      selfname:item.name,
    })
  },
  // 隐藏弹窗
  closeLogPopupTap(){
    this.setData({
      hideLogPopup:true
    })
  },
  // input
  inputBlur(e){
    this.setData({
      product_name:e.detail.value,
      page:1,
      goodsLists:[],
    })
    this.getGoodsLists();
  },
  // 排序更改
  changeSort(e){
    let type = e.currentTarget.dataset.type;
    this.setData({
      sort:type,
      page:1,
      goodsLists:[]
    })
    this.getGoodsLists();
  },
  /**
   * 生命周期函数--监听页面加载
   * 
   */
  onLoad: function (options) {
    // this.getGoodsLists()
    this.getCategory(0);
    this.changePriceTemplate();
  },
  // 获取分类
  getCategory(type,parent_id){
    var that = this;
    wx.request({
      url: app.globalData.urls + '/product/category',
      method: 'GET',
      data: {
        parent_id
      },
      header: app.getPostRequestHeader(),
      success: function (res) {
        if(res.data.code==200){
          let data = res.data.data,dataId={},dataArr=[];
          dataArr = data.map(val=>{
            dataId[val.name] = val.id
            return val.name;
          })
          that.setData({
            ["categoryId["+type+"]"]:dataId,
            ["category["+type+"]"]:data
          })
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({
      page:1,
      isSelect:false,
      goodsLists:[],
      product_name:'',
    })
    
    this.getGoodsLists();
  },
  // 是否是新品更换
  changeNew(){
    this.setData({
      isnew:this.data.isnew==2 ? 1 :2,
      page:1,
      ["this.data.selectCate[2]"]:null,
      product_name:'',
      goodsLists:[],
      isSelect:false,
      is_hot:1
    })
    
    this.getGoodsLists();
  },
  
  // 是否是爆品推荐
  changeHot(){
    
    this.setData({
      isnew:1,
      is_hot:this.data.is_hot==2 ? 1 :"",
      page:1,
      ["this.data.selectCate[2]"]:null,
      product_name:'',
      goodsLists:[],
      isSelect:false
    })
    
    this.getGoodsLists();
  },
  // 编辑商品
  edit(e){
    let item=e.currentTarget.dataset.item;
    wx.navigateTo({
      url: '/pages/business/add-goods/add-goods?id='+item.id,
    })
  },
  // 单选
  singleSelect(e){
    let dataset=e.currentTarget.dataset,getItem = dataset.item,index = dataset.index;
    getItem.selected = !getItem.selected
    this.setData({
      ['goodsLists['+index+']']:getItem
    })
  },
  // 商品列表
  getGoodsLists(){
    var that = this;
   
    wx.request({
      url: app.globalData.urls + '/product/product-list',
      method: 'GET',
      data: {
        page:this.data.page,
        bdmin_user_id:util.getBdmin_user_id(),
        goods_type:this.data.selfType,//类型 1社交电商自营 2社区团购自营 3天天精选商品库
        status:Number(this.data.currentTab)+1,//上下架状态1 上架 2下架
        category_id:this.data.categoryId[2] && this.data.categoryId[2][this.data.selectCate[2]] || '',
        ...(this.data.isnew=='' ? {} : {is_new:this.data.isnew}),
        ...(this.data.is_hot=='' ? {} : {is_hot:this.data.is_hot}),
        product_name:this.data.product_name,
        order_field:this.data.sort,
      },
      header: app.getPostRequestHeader(),
      success: function (res) {
        if(res.data.code==200){
          let data=res.data.data,goodsLists = that.data.goodsLists.concat(data)
          that.setData({
            goodsLists: goodsLists,
            isContain:data.length==0
          })
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if(!this.data.isContain)  {
      this.setData({
        page:Number(this.data.page)+1
      })
      this.getGoodsLists();
    }
  },
})